class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable#, :confirmable
    has_many :rooms
    has_many :messages
    has_many :goods, dependent: :destroy
    has_many :orders, dependent: :destroy
    # attr_accessor :name, :contact, :region, :note, :level
    has_attached_file :qrcode, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "qrcode_default.png"
    validates_attachment_content_type :qrcode, content_type: /\Aimage\/.*\z/
    validates :username, :region, :email, :password, presence: true, on: :create
    
    validates_uniqueness_of :username, :email
    validates :email, format: { with: /\A[^@\s]+@([^@\s]+\.)+ac\.cn\z/, message: "应为中科院邮箱" }
    scope :all_except, ->(user) { where.not(id: user) }
    # after_create_commit { broadcast_append_to "users" }
    
  def admin?
    self.role == "admin"
  end
end
